import request from '../../utils/request'

Page({
  data: {
    id: null,
    post: null,
    comments: [],
    commentContent: '',
    isLiked: false,
    page: 1,
    limit: 10,
    hasMore: true,
    backendUrl: 'http://127.0.0.1:88/api/user/'
  },

  onLoad(options) {
    const { id } = options;
    this.setData({ id });
    this.loadPostDetail();
    this.loadComments();
  },

  // 加载帖子详情
  loadPostDetail() {
    wx.showLoading({ title: '加载中...' });
    wx.request({
      url: `${this.data.backendUrl}post.php`,
      method: 'GET',
      data: { 
        action: 'detail',
        id: this.data.id 
      },
      success: (res) => {
        if (res.data.status === 'success') {
          const post = res.data.post;
          // 处理图片路径
          if (post.avatar) {
            post.avatar = this.data.backendUrl + post.avatar;
          }
          if (post.image) {
            post.image = post.image;
          }
          this.setData({ post });
        }
      },
      complete: () => {
        wx.hideLoading();
      }
    });
  },

  // 加载评论表
  loadComments() {
    const { id, page, limit } = this.data;
    wx.request({
      url: `${this.data.backendUrl}post.php`,
      method: 'GET',
      data: { 
        action: 'comments',
        post_id: id,
        page,
        limit
      },
      success: (res) => {
        if (res.data.status === 'success') {
          const comments = res.data.comments;
          // 处理评论用户头像
          comments.forEach(comment => {
            if (comment.avatar) {
              comment.avatar = this.data.backendUrl + comment.avatar;
            }
          });
          this.setData({
            comments: page === 1 ? comments : [...this.data.comments, ...comments],
            hasMore: comments.length === limit
          });
        }
      }
    });
  },

  // 处理评论输入
  handleCommentInput(e) {
    this.setData({
      commentContent: e.detail.value
    });
  },

  // 提交评论
  submitComment() {
    const { commentContent, id } = this.data;
    if (!commentContent.trim()) {
      wx.showToast({
        title: '请输入评论内容',
        icon: 'none'
      });
      return;
    }

    const userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }

    wx.request({
      url: `${this.data.backendUrl}post.php`,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        action: 'addComment',
        post_id: id,
        user_id: userInfo.id,
        content: commentContent
      },
      success: (res) => {
        if (res.data.status === 'success') {
          wx.showToast({
            title: '评论成功',
            icon: 'success'
          });
          // 重置评论列表并重新加载
          this.setData({
            commentContent: '',
            page: 1,
            comments: []
          }, () => {
            this.loadComments();
            this.loadPostDetail(); // 刷新评论数
          });
        } else {
          wx.showToast({
            title: res.data.message || '评论失败',
            icon: 'none'
          });
        }
      }
    });
  },

  // 处理点赞
  handleLike() {
    const userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }

    const { id, isLiked, post } = this.data;
    wx.request({
      url: `${this.data.backendUrl}post.php`,
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        action: 'toggleLike',
        post_id: id,
        user_id: userInfo.id
      },
      success: (res) => {
        if (res.data.status === 'success') {
          this.setData({
            isLiked: !isLiked,
            'post.like_count': isLiked ? post.like_count - 1 : post.like_count + 1
          });
        }
      }
    });
  },

  // 预览图片
  previewImage() {
    const { post, backendUrl } = this.data;
    if (post.image) {
      wx.previewImage({
        urls: [backendUrl + post.image]
      });
    }
  },

  // 上拉加载更多评论
  onReachBottom() {
    if (this.data.hasMore) {
      this.setData({
        page: this.data.page + 1
      }, () => {
        this.loadComments();
      });
    }
  },

  // 分享
  onShareAppMessage() {
    const { post } = this.data;
    return {
      title: post.title,
      path: `/pages/post-detail/post-detail?id=${post.id}`,
      imageUrl: post.image
    };
  }
}); 